package demo.thread;

public class AtomicityDemo {

    private static int index = 0;

    public static void main(String[] args) throws InterruptedException {
        int numThreads = 2;
        int count = 100000;

        Thread[] threads = new Thread[numThreads];

        for (int i = 0; i < numThreads; i++) {
            threads[i] = new Thread(() -> {
                for (int j = 0; j < count; j++) {
                    index++;
                }
            });
            threads[i].start();
        }

        for (Thread thread : threads) {
            thread.join();
        }

        System.out.println("Final value of index = " + index);
        System.out.println("Expected value = " + (numThreads * count));
    }
}
